#include "OptionsMenuGui.h"

#include <Gwen/Controls/TabControl.h>
#include <Gwen/Controls/ComboBox.h>

OptionsMenuGui::OptionsMenuGui(Gwen::Controls::Base *a_pParent, PF::Window *a_pWindow)
	: Gwen::Controls::WindowControl(a_pParent), m_pWindow(a_pWindow)
{
	SetSize(640, 480);
	SetTitle(L"Options");
	Gwen::Align::Center(this);

	Gwen::Controls::TabControl *pTabControl = new Gwen::Controls::TabControl(this);
	pTabControl->Dock(Gwen::Pos::Fill);
	pTabControl->SetMargin(Gwen::Margin(2, 2, 2, 2));

	Gwen::Controls::TabButton *pVideoTabButton = pTabControl->AddPage(L"Video");
	Gwen::Controls::Base *pVideoPage = pVideoTabButton->GetPage();

	Gwen::Controls::Label *pResolutionLabel = new Gwen::Controls::Label(pVideoPage);;
	pResolutionLabel->SetText(L"Resolution:");
	pResolutionLabel->SizeToContents();

	Gwen::Controls::ComboBox *pResolutionComboBox = new Gwen::Controls::ComboBox(pVideoPage);
	pResolutionComboBox->SetPos(89, 0);

	Gwen::Controls::Label *pDisplayModeLabel = new Gwen::Controls::Label(pVideoPage);
	pDisplayModeLabel->SetText(L"Display Mode:");
	pDisplayModeLabel->SizeToContents();
	pDisplayModeLabel->SetPos(0, 25);

	Gwen::Controls::ComboBox *pDisplayModeComboBox = new Gwen::Controls::ComboBox(pVideoPage);
	pDisplayModeComboBox->SetPos(89, 25);
	pDisplayModeComboBox->AddItem(L"Windowed");
	pDisplayModeComboBox->AddItem(L"Fullscreen");
	pDisplayModeComboBox->onSelection.Add(this, &OptionsMenuGui::OnDisplayModeItemSelected);

	int iDisplayIndex = SDL_GetWindowDisplay(m_pWindow->GetHandle());
	SDL_DisplayMode kMode;
	Gwen::String szName;

	for(int i = 0; i < SDL_GetNumDisplayModes(iDisplayIndex); ++i)
	{
		SDL_GetDisplayMode(iDisplayIndex, i, &kMode);

		Gwen::UnicodeString szLabel = Gwen::Utility::Format(L"%dx%d", kMode.w, kMode.h);
		szName = Gwen::Utility::UnicodeToString(szLabel);
		if(pResolutionComboBox->FindChildByName(szName, true) == NULL)
		{
			pResolutionComboBox->AddItem(szLabel, szName);
		}
	}

	szName = Gwen::Utility::UnicodeToString(Gwen::Utility::Format(L"%dx%d", kMode.w, kMode.h));
	Gwen::Controls::Base *pControl = pResolutionComboBox->FindChildByName(szName, true);
	pResolutionComboBox->OnItemSelected(pControl);
	pResolutionComboBox->onSelection.Add(this, &OptionsMenuGui::OnResolutionItemSelected);
}

void OptionsMenuGui::OnResolutionItemSelected(Gwen::Controls::Base *a_pControl)
{
	Gwen::Controls::ComboBox *pComboBox = (Gwen::Controls::ComboBox *) a_pControl;
	Gwen::Controls::Label *pLabel = pComboBox->GetSelectedItem();
	Gwen::UnicodeString szText = pLabel->GetText();

	int iW, iH;
	wchar_t cBuffer;

	std::wstringstream kStringStream;
	kStringStream << szText;
	kStringStream >> iW >> cBuffer >> iH;

	m_pWindow->SetSize(iW, iH);
}

void OptionsMenuGui::OnDisplayModeItemSelected(Gwen::Controls::Base *a_pControl)
{
	Gwen::Controls::ComboBox *pComboBox = (Gwen::Controls::ComboBox *) a_pControl;
	Gwen::Controls::Label *pLabel = pComboBox->GetSelectedItem();

	m_pWindow->SetFullscreen(pLabel->GetText() == L"Fullscreen");
}

PF::Window *OptionsMenuGui::GetWindow()
{
	return m_pWindow;
}
